home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / gfx / misc / uplot.lha / plot.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  7KB  |  357 lines

  1. /* A program to plot UNIX plot files on an Amiga screen */
  2. /*
  3. /* Richard Gerber 5/5/92  gerber@sirius.astro.uiuc.edu */
  4. /* Do with this what you wish (like add error checking! and proper font
  5.    support) */
  6. /* Compiled with SAS/C 5.10b w/ 2.04 include files (I think it should 
  7.    work with 1.3) */
  8.  
  9. #include <stdio.h>
  10. #include <exec/types.h>
  11. #include <intuition/intuition.h>
  12. #include <proto/intuition.h>
  13. #include <proto/graphics.h>
  14. #include <graphics/gfxmacros.h>
  15.  
  16. typedef short int INT16;
  17.  
  18. void readplot(char *);
  19. void opengraphics();
  20. void myexit(char *);
  21. UBYTE handleIDCMP( struct Window *);
  22. void erase();
  23. void line(INT16, INT16, INT16, INT16);
  24. void lineto(INT16, INT16);
  25. void move(INT16, INT16);
  26. void dotext(INT16, INT16, char *);
  27. void reverse(char *, int);
  28. void setlinemode(char *);
  29. void circle(INT16, INT16, INT16);
  30. void arc(void);
  31.  
  32.  
  33. INT16 scale = 1;
  34. char text[81], linemod[40];
  35. struct Window *window=NULL;
  36. struct GfxBase *GfxBase=NULL;
  37. struct IntuitionBase *IntuitionBase=NULL;
  38. struct Screen screen;
  39. int wdth,ht;
  40. int zeroy;
  41. char fname[40];
  42.  
  43. struct NewWindow newwindow = 
  44. {
  45.     0,0,0,0,   /* filled in later */
  46.     -1,-1,
  47.     CLOSEWINDOW|NEWSIZE,
  48.     WINDOWCLOSE|WINDOWDEPTH|SMART_REFRESH|WINDOWDRAG
  49.     |WINDOWSIZING,
  50.     NULL, NULL,
  51.     "UNIX Plot",
  52.     NULL, NULL,
  53.     100,50,
  54.     ~0,~0,
  55.     WBENCHSCREEN
  56. };    
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.     ULONG signalmask, signals;
  61.     UBYTE done = 0;
  62.  
  63.     if(argc!=2) {printf("Usage: %s plot_file.\n",argv[0]); exit(1);}
  64.  
  65.     opengraphics();
  66.  
  67.     strcpy(fname,argv[1]);    
  68.     readplot(argv[1]); 
  69.  
  70.     signalmask = 1L << window->UserPort->mp_SigBit;
  71.     while ( !done )
  72.     {
  73.         signals = Wait(signalmask);
  74.         if (signals & signalmask)
  75.             done = handleIDCMP(window);
  76.     };        
  77.     
  78.     myexit(NULL);
  79. }
  80.  
  81. void
  82. readplot(char *filename)
  83. {
  84.     FILE *fp=NULL;
  85.     int n;
  86.     INT16 curx, cury;
  87.     unsigned char com;
  88.     INT16 pts[6];
  89.     
  90.  
  91.     fp = fopen(filename,"rb");
  92.     if(fp==NULL) myexit("Can't open file");
  93.  
  94.  
  95.     while(!feof(fp))
  96.     {
  97.         n = fscanf(fp,"%c",&com);
  98.         if(n==EOF) break;
  99.         switch(com) {
  100.             case 'e':
  101.                 erase();
  102.                 break;
  103.             case 's':
  104.                 fread((char *)pts,8,1,fp);
  105.                 reverse((char *)pts,4);
  106.                 scale = pts[2];
  107.                 if(scale!=pts[3]) 
  108.                     printf("Error: scale not square!\n");
  109.                 break;
  110.             case 'l':
  111.                 fread((char *)pts,8,1,fp);
  112.                 reverse((char *)pts,4);
  113.                 curx = pts[2]; cury = pts[3];
  114.                 line(pts[0],pts[1],pts[2],pts[3]);
  115.                 break;
  116.             case 'n':
  117.                 fread((char *)pts,4,1,fp);
  118.                 reverse((char *)pts,2);
  119.                 lineto(pts[0],pts[1]);
  120.                 curx = pts[0]; cury = pts[1];
  121.                 break;
  122.             case 'm':
  123.                 fread((char *)pts,4,1,fp);
  124.                 reverse((char *)pts,2);
  125.                 curx = pts[0]; cury = pts[1];
  126.                 move(curx,cury);
  127.                 break;
  128.             case 't':
  129.                 fgets(text,80,fp);
  130.                 dotext(curx,cury,text);
  131.                 break;    
  132.             case 'f':
  133.                 fgets(linemod,40,fp);
  134.                 setlinemode(linemod);
  135.                 break;    
  136.             case 'c':
  137.                 fread((char *)pts,6,1,fp);
  138.                 reverse((char *)pts,3);
  139.                 circle(pts[0],pts[1],pts[2]);
  140.                 break;    
  141.             case 'a':
  142.                 fread((char *)pts,12,1,fp);
  143.                 arc();
  144.                 break;
  145.             default:
  146.                 printf("Unknown commnd %c,\n",com);
  147.                 myexit("Error in data file?");
  148.                 break;
  149.             break;
  150.         }
  151.     }
  152.     fclose(fp);
  153. }
  154.  
  155. void
  156. opengraphics(void)
  157. {
  158.     IntuitionBase = (struct IntuitionBase *)OpenLibrary(
  159.             "intuition.library",33L);
  160.     if(IntuitionBase==NULL) myexit("Intuition Library");
  161.  
  162.     GfxBase = (struct GfxBase *)OpenLibrary(
  163.             "graphics.library",33L);
  164.     if(GfxBase==NULL) myexit("Graphics Library");
  165.  
  166.     GetScreenData((CPTR *)&screen, sizeof(struct Screen),
  167.             WBENCHSCREEN, NULL);
  168.     
  169.     newwindow.LeftEdge = 0;
  170.     newwindow.TopEdge = 0;
  171.     newwindow.Width = screen.Width;
  172.     newwindow.Height = screen.Height;
  173.     ht =  screen.Height - 2*screen.BarHeight;
  174.     wdth = screen.Width - 2*screen.BarHeight;
  175.     zeroy = screen.Height - 5;
  176.  
  177.     window = (struct Window *)OpenWindow(&newwindow);
  178.     if(window==NULL) myexit("OpenWindow()");
  179. }
  180.  
  181. void myexit(char *message)
  182. {
  183.     if(message!=NULL) printf("Program ending: %s error\n",message);
  184.  
  185.     if(window) CloseWindow(window);
  186.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  187.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  188.     exit(1);
  189. }
  190. UBYTE handleIDCMP( struct Window *w)
  191. {
  192.     UBYTE flag = 0;
  193.     struct IntuiMessage *message = NULL;
  194.     ULONG class;
  195.  
  196.     while(message = (struct IntuiMessage *)GetMsg(w->UserPort) )
  197.     {
  198.         class = message->Class;
  199.         ReplyMsg( (struct Message *)message);
  200.  
  201.         switch (class)
  202.         {
  203.             case CLOSEWINDOW:
  204.                 flag = 1;
  205.                 break;
  206.             case NEWSIZE:
  207.                 wdth = window->Width - 2*screen.BarHeight;
  208.                 ht = window->Height - 2*screen.BarHeight;
  209.                 zeroy = window->Height - 5;
  210.                 erase();
  211.                 readplot(fname);
  212.                 break;
  213.             default:
  214.                 break;
  215.         }
  216.     }
  217.     return(flag);
  218. }
  219.  
  220. void erase(void)
  221. {
  222.     SetAPen(window->RPort,0);
  223.     RectFill(window->RPort,0,0,
  224.         window->Width,window->Height);    
  225.     RefreshWindowFrame(window);    
  226. }
  227.  
  228. void
  229. line(INT16 x0, INT16 y0, INT16 x1, INT16 y1)
  230. {
  231.     int xa, ya, xb, yb;
  232.     xa = (x0*wdth)/scale ;
  233.     xb = (x1*wdth)/scale ;
  234.     ya = -(y0*ht)/scale + zeroy;
  235.     yb = -(y1*ht)/scale + zeroy;
  236.     SetAPen(window->RPort, 1);
  237.     Move(window->RPort,xa,ya);
  238.     Draw(window->RPort,xb,yb);
  239. }
  240.  
  241. void lineto(INT16 x1, INT16 y1)
  242. {
  243.     int xa, ya;
  244.     xa = (x1*wdth)/scale ;
  245.     ya = -(y1*ht)/scale + zeroy;
  246.     
  247.     SetAPen(window->RPort, 1);
  248.     Draw(window->RPort,xa,ya);
  249. }
  250.  
  251. void move(INT16 x0, INT16 y0)
  252. {
  253.     int xa, ya;
  254.     xa = (x0*wdth)/scale ;
  255.     ya = -(y0*ht)/scale + zeroy;
  256.     Move(window->RPort,xa,ya);
  257. }
  258.  
  259. void circle(INT16 x0, INT16 y0, INT16 radius)
  260. {
  261.     int xa,ya,rx,ry;
  262.     xa = (x0*wdth)/scale;
  263.     ya = -(y0*ht)/scale + zeroy;
  264.     ry = radius*ht/scale;
  265.     rx = radius*wdth/scale;
  266.     DrawEllipse(window->RPort,xa,ya,rx,ry);
  267. }
  268.  
  269. void arc(void)
  270. {
  271.     printf("Arc command not implemented.\n");
  272. }
  273.  
  274.  
  275. void dotext(INT16 x0, INT16 y0, char *txt)
  276. {
  277.     int length;
  278.     char strip[80];
  279.     int xa, ya;
  280.     int i;
  281.     int textheight = 8;
  282.     struct IntuiText it = {
  283.     2,0,  JAM1, 1,1, NULL, NULL, NULL };
  284.     USHORT holdptrn;
  285.  
  286.      holdptrn = window->RPort->LinePtrn;    
  287.     window->RPort->LinePtrn = 0xFFFF;    
  288.  
  289.     for(i=0;i<80;i++) strip[i]='\0';
  290.  
  291.     xa = (x0*wdth)/scale ;
  292.     ya = -(y0*ht)/scale + zeroy - textheight/2;
  293.     length = strlen(txt);
  294.     strncpy(strip,txt,length-1);
  295.  
  296.     it.IText = strip;    
  297.     PrintIText(window->RPort, &it, xa, ya);
  298.  
  299.     window->RPort->LinePtrn = holdptrn;
  300. }
  301.  
  302. void
  303. reverse(char array[], int i)
  304. /*  99% of the time spent writing this program */
  305. /* was spent figuring out that the bytes are reversed in the plot file */
  306. /* This routine puts them back */
  307. /* i is the actual number of short ints passed */
  308. {
  309.     int j;
  310.  
  311.     char fix[12];
  312.  
  313.     for(j=0;j<2*i;j++) fix[j] = array[j];
  314.  
  315.     for(j=0;j<2*i;j+=2)
  316.     {
  317.         array[j] = fix[j+1];
  318.         array[j+1] = fix[j];
  319.     }
  320. }
  321.  
  322. void
  323. setlinemode(char *t)
  324. {
  325.     int result, length;
  326.     char strip[20];
  327.  
  328.         length = strlen(t);
  329.         strncpy(strip,t,length-1);
  330.     strip[length-1] = '\0';
  331.  
  332.     if(0==(result=strcmp(strip,"solid")))
  333.     {
  334.         window->RPort->LinePtrn = 0xFFFF;
  335.     }
  336.     
  337.     if(0==(result=strcmp(strip,"longdashed")))
  338.     {
  339.         window->RPort->LinePtrn = 0xFF00;
  340.     }
  341.  
  342.     if(0==(result=strcmp(strip,"dotted")))
  343.     {
  344.         window->RPort->LinePtrn = 0xCCCC;
  345.     }
  346.     
  347.     if(0==(result=strcmp(strip,"shortdashed")))
  348.     {
  349.         window->RPort->LinePtrn = 0xF0F0;
  350.     }
  351.  
  352.     if(0==(result=strcmp(strip,"dotdashed")))
  353.     {
  354.         window->RPort->LinePtrn = 0xF6F6;
  355.     }
  356. }    
  357.